package com.summer.peony.system.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.summer.peony.system.mapper.SysDictMapper;
import com.summer.peony.system.pojo.SysDict;
import com.summer.peony.system.pojo.SysDictExample;
import com.summer.peony.system.pojo.SysDictExample.Criteria;
import com.summer.peony.system.service.SysDictService;

/**
 * @ClassName: SysDictServiceImpl
 * @Description: TODO(字典实现类)
 * @author: TaoGe
 * @date: 2021年1月25日 上午11:40:49
 * @version: v0.0.1
 */
@Transactional
@Service
public class SysDictServiceImpl implements SysDictService {

	@Autowired
	private SysDictMapper sysDictMapper;

	@Override
	public Integer add(SysDict sysDict) {
		// 新增字典
		Date date = new Date();
		sysDict.setCreateTime(date);
		sysDict.setUpdateTime(date);
		sysDict.setDelFlag("0");
		return sysDictMapper.insert(sysDict);
	}

	@Override
	public Integer edit(SysDict sysDict) {
		// 编辑字典
		SysDict sysDict2 = sysDictMapper.selectByPrimaryKey(sysDict.getId());
		sysDict2.setLabel(sysDict.getLabel());
		sysDict2.setValue(sysDict.getValue());
		sysDict2.setRemarks(sysDict.getRemarks());
		sysDict2.setSort(sysDict.getSort());
		sysDict2.setType(sysDict.getType());
		sysDict2.setDescription(sysDict.getDescription());
		Date date = new Date();
		sysDict2.setUpdateTime(date);
		return sysDictMapper.updateByPrimaryKey(sysDict2);
	}

	@Override
	public Integer remov(Integer id) {
		// 删除字典
		return sysDictMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PageInfo<SysDict> list(String label, String value, String type, Integer pageNum, Integer pageSize) {
		// 分页查询字典
		SysDictExample example = new SysDictExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagEqualTo("0").andLabelLike("%" + label + "%").andValueLike("%" + value + "%")
				.andTypeLike("%" + type + "%");
		PageHelper.startPage(pageNum, pageSize);
		List<SysDict> list = sysDictMapper.selectByExample(example);
		PageInfo<SysDict> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

}
